<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>姓名限制功能测试</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .test-section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
        }
        .test-section h3 {
            color: #333;
            margin-top: 0;
        }
        input[type="text"] {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            margin: 10px 0;
        }
        input.error {
            border-color: #ff6b6b;
            box-shadow: 0 0 5px rgba(255, 107, 107, 0.3);
        }
        button {
            background: #4CAF50;
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin: 5px;
        }
        button:hover {
            background: #45a049;
        }
        button:disabled {
            background: #ccc;
            cursor: not-allowed;
        }
        .error-message {
            color: #ff6b6b;
            background: rgba(255, 107, 107, 0.1);
            padding: 10px;
            border-radius: 5px;
            margin: 10px 0;
            border-left: 4px solid #ff6b6b;
        }
        .success-message {
            color: #4CAF50;
            background: rgba(76, 175, 80, 0.1);
            padding: 10px;
            border-radius: 5px;
            margin: 10px 0;
            border-left: 4px solid #4CAF50;
        }
        .log {
            background: #f8f9fa;
            border: 1px solid #e9ecef;
            border-radius: 5px;
            padding: 15px;
            margin: 10px 0;
            max-height: 300px;
            overflow-y: auto;
            font-family: monospace;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🧪 姓名限制功能测试</h1>
        
        <div class="test-section">
            <h3>📝 客户端测试</h3>
            <p>测试前端本地存储的姓名限制功能</p>
            
            <input type="text" id="testName" placeholder="请输入测试姓名" />
            <div id="nameError" class="error-message" style="display: none;"></div>
            
            <button onclick="testClientSide()">测试姓名可用性</button>
            <button onclick="recordNameUsage()">锁定当前姓名</button>
            <button onclick="clearLocalStorage()">清除锁定记录</button>
        </div>
        
        <div class="test-section">
            <h3>📊 测试日志</h3>
            <div id="testLog" class="log"></div>
            <button onclick="clearLog()">清除日志</button>
        </div>
        
        <div class="test-section">
            <h3>🔍 本地存储查看</h3>
            <button onclick="showLocalStorage()">查看本地存储</button>
            <div id="storageDisplay" class="log" style="display: none;"></div>
        </div>
    </div>

    <script>
        // 日志函数
        function log(message) {
            const logDiv = document.getElementById('testLog');
            const timestamp = new Date().toLocaleTimeString();
            logDiv.innerHTML += `[${timestamp}] ${message}<br>`;
            logDiv.scrollTop = logDiv.scrollHeight;
        }

        function clearLog() {
            document.getElementById('testLog').innerHTML = '';
        }

        // 姓名锁定检查函数（新逻辑）
        function checkNameAvailability(name) {
            if (!name) {
                return { available: true, error: '', remainingTime: 0, lockedName: null };
            }

            // 获取本地存储的锁定姓名记录
            const lockedNameData = JSON.parse(localStorage.getItem('lockedName') || 'null');
            const currentTime = Date.now();
            const timeLimit = 30 * 60 * 1000; // 30分钟

            // 检查是否有锁定的姓名
            if (lockedNameData) {
                const { name: lockedName, timestamp } = lockedNameData;
                const timeDiff = currentTime - timestamp;

                if (timeDiff < timeLimit) {
                    // 还在锁定期内
                    if (name === lockedName) {
                        // 输入的是锁定的姓名，允许使用
                        return { available: true, error: '', remainingTime: 0, lockedName: lockedName };
                    } else {
                        // 输入的不是锁定的姓名，不允许使用
                        const remainingSeconds = Math.ceil((timeLimit - timeDiff) / 1000);
                        const minutes = Math.floor(remainingSeconds / 60);
                        const seconds = remainingSeconds % 60;
                        const timeStr = minutes > 0 ? `${minutes}分${seconds}秒` : `${seconds}秒`;

                        return {
                            available: false,
                            error: `当前姓名已锁定为「${lockedName}」，请等待 ${timeStr} 后才能使用其他姓名，或直接使用「${lockedName}」`,
                            remainingTime: remainingSeconds,
                            lockedName: lockedName
                        };
                    }
                } else {
                    // 超过30分钟，清除锁定记录
                    localStorage.removeItem('lockedName');
                }
            }

            // 没有锁定的姓名或锁定已过期，任何姓名都可以使用
            return { available: true, error: '', remainingTime: 0, lockedName: null };
        }

        // 测试客户端逻辑
        function testClientSide() {
            const name = document.getElementById('testName').value.trim();
            if (!name) {
                alert('请输入姓名');
                return;
            }

            log(`测试姓名: "${name}"`);
            const result = checkNameAvailability(name);
            
            const errorDiv = document.getElementById('nameError');
            const nameInput = document.getElementById('testName');
            
            if (result.available) {
                log(`✅ 姓名 "${name}" 可以使用`);
                errorDiv.style.display = 'none';
                nameInput.classList.remove('error');
            } else {
                log(`❌ 姓名 "${name}" 不可使用: ${result.error}`);
                errorDiv.textContent = result.error;
                errorDiv.style.display = 'block';
                nameInput.classList.add('error');
            }
        }

        // 锁定姓名
        function recordNameUsage() {
            const name = document.getElementById('testName').value.trim();
            if (!name) {
                alert('请输入姓名');
                return;
            }

            // 检查是否已有锁定的姓名
            const lockedNameData = JSON.parse(localStorage.getItem('lockedName') || 'null');
            if (!lockedNameData) {
                const lockData = {
                    name: name,
                    timestamp: Date.now()
                };
                localStorage.setItem('lockedName', JSON.stringify(lockData));
                log(`🔒 锁定姓名为 "${name}"`);
            } else {
                log(`⚠️ 姓名已锁定为 "${lockedNameData.name}"，无法更改`);
            }

            showLocalStorage();
        }

        // 清除本地存储
        function clearLocalStorage() {
            localStorage.removeItem('lockedName');
            localStorage.removeItem('usedNames'); // 清除旧的记录
            log('🗑️ 清除本地存储');
            showLocalStorage();

            // 清除错误提示
            document.getElementById('nameError').style.display = 'none';
            document.getElementById('testName').classList.remove('error');
        }

        // 显示本地存储
        function showLocalStorage() {
            const lockedNameData = JSON.parse(localStorage.getItem('lockedName') || 'null');
            const storageDiv = document.getElementById('storageDisplay');

            if (!lockedNameData) {
                storageDiv.innerHTML = '本地存储为空，没有锁定的姓名';
            } else {
                const { name, timestamp } = lockedNameData;
                const date = new Date(timestamp);
                const timeStr = date.toLocaleString();
                const timeDiff = Date.now() - timestamp;
                const remainingTime = Math.max(0, 30 * 60 * 1000 - timeDiff);
                const remainingMinutes = Math.ceil(remainingTime / 1000 / 60);

                let html = '<strong>锁定的姓名:</strong><br>';
                html += `🔒 ${name}: ${timeStr}`;
                if (remainingTime > 0) {
                    html += ` (剩余 ${remainingMinutes} 分钟)`;
                } else {
                    html += ` (已过期)`;
                }

                storageDiv.innerHTML = html;
            }

            storageDiv.style.display = 'block';
        }

        // 页面加载时显示本地存储
        window.onload = function() {
            log('页面加载完成，开始测试');
            showLocalStorage();
        };
    </script>
</body>
</html>
